namespace PHSRAG.Insight21.Common.CommonControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using Header = PHSRAG.Insight21.InsightWebControls.Header;

	/// <summary>
	///		Summary description for HelpDocumentation.
	/// </summary>
	public partial class HelpDocumentation : System.Web.UI.UserControl
	{

		#region Events
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				//@@@JGCwhy doesn't this work for setting the title???
//				HtmlGenericControl c = ((HtmlGenericControl)Page.FindControl("pageTitle"));
//				c.InnerText = "Insight 2.1 Documentation";

            string helpFileName = string.Empty;
            string moduleName = ""; //88888 = Header.ActiveModule;

            if (moduleName == InsightUtilities.Constants.Modules.Admin)
            {
               helpFileName = Context.Server.MapPath(Utility.Settings.GetKeyValue("Help.Admin.Documentation.File", string.Empty));
            }
            else if (moduleName == InsightUtilities.Constants.Modules.ClinicalTrials)
            {
               helpFileName = Context.Server.MapPath(Utility.Settings.GetKeyValue("Help.ClinicalTrials.Documentation.File", string.Empty));
            }
            else
            {
               helpFileName = Context.Server.MapPath(Utility.Settings.GetKeyValue("Help.Default.Documentation.File", string.Empty));
            }
					
				Response.ContentType = Utility.Settings.GetKeyValue("Help.Documentation.File.Type", string.Empty);

				using (FileStream fileStream = new FileStream(helpFileName, FileMode.Open, FileAccess.Read))
				{
					byte[] bytes = new byte[fileStream.Length];
					int totalBytesToRead = (int)fileStream.Length;
					int totalBytesRead = 0;

					while (totalBytesToRead > 0) 
					{
						int bytesRead = fileStream.Read(bytes, totalBytesRead, totalBytesToRead);
						if (bytesRead == 0)
							break;
						totalBytesRead += bytesRead;
						totalBytesToRead -= bytesRead;
					}

					Page.Response.OutputStream.Write(bytes, 0, bytes.Length);
				}
			}
			Response.End();
		}
		#endregion 

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);

		}
		#endregion

	}
}
